package eerbaar.v2.gamebuilder.client.request;

import org.apache.http.client.methods.HttpPost;

public class Requester {

    private final static Requester instance = new Requester();

    public static Requester getInstance() {
        return instance;
    }

    private IRequestProcessor processor;

    private Requester() {
        //
    }

    public boolean needsProcessing() {

        if (this.processor == null) {
            return false;
        }

        return this.processor.needsProcessing();
    }

    public void processInGameLoop() {

        if (this.processor == null) {
            return;
        }

        if (this.processor.needsProcessing() == false) {
            return;
        }

        this.processor.processInGameLoop();
        this.processor = null;
    }

    public void start(IRequestProcessor processor, RequestType type) {

        this.processor = processor;

        HttpPost method = type.getHttpPost();
        RequestRunner runner = new RequestRunner(method, processor);

        Thread thread = new Thread(runner);
        thread.start();
    }

}
